package com.jcark.utilscode.glide

import android.content.Context
import android.graphics.drawable.Drawable
import android.text.TextUtils
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions

/**
 * Glide工具类
 */
object GlideUtils {
    private var placeholderDrawable: Drawable? = null

    private fun getPlaceholder(context: Context, id: Int): Drawable? {
        if (placeholderDrawable == null) {
            placeholderDrawable = androidx.core.content.ContextCompat.getDrawable(context, id)
        }
        return placeholderDrawable
    }

    /**
     * 加载网络图片
     *
     * @param url 图片地址
     * @return DrawableRequestBuilder<String>
    </String> */
    fun loaderImg(url: Any, view: ImageView, context: Context, id: Int) {
        val options = RequestOptions()
        Glide.with(view)
            .load(url)
            .placeholder(getPlaceholder(context, id))
            .error(getPlaceholder(context, id))
            .apply(options)
            .dontAnimate()
            .into(view)
    }

    /**
     * 加载网络图片无占位图
     *
     * @param url 图片地址
     * @return DrawableRequestBuilder<String>
    </String> */
    fun loaderImgNoPlaceholder(url: Any, view: ImageView) {
        val options = RequestOptions()
        Glide.with(view)
            .load(url)
            .apply(options)
            .dontAnimate()
            .into(view)
    }

    fun loaderImg(url: Any, view: ImageView, placeholderId: Int) {
        val options = RequestOptions()
        Glide.with(view)
            .load(url)
            .placeholder(placeholderId)
            .error(placeholderId)
            .apply(options)
            .dontAnimate()
            .into(view) //加载图
    }

    /**
     * 图片加载
     *
     * @param url           图片url
     * @param view          图片控件
     * @param placeholderId 默认图
     */
    fun loadImage(
        url: String,
        view: ImageView,
        @androidx.annotation.DrawableRes placeholderId: Int
    ) {
        if (TextUtils.isEmpty(url)) {
            view.setImageResource(placeholderId)
        } else {
            val options = RequestOptions()
            Glide.with(view)
                .load(url)
                .placeholder(placeholderId)
                .error(placeholderId)
                .apply(options)
                .dontAnimate()
                .into(view)
        }
    }

    fun loaderImg(res: Int, view: ImageView) {
        val options = RequestOptions()
        Glide.with(view)
            .load(res)
            .apply(options)
            .into(view) //加载图
    }

    /**
     * 加载图有-------圆角
     */
    fun loaderImgCorners(url: Any, view: ImageView, radius: Int, context: Context, id: Int) {
        val options: RequestOptions = RequestOptions()
            .apply(RequestOptions.bitmapTransform(RoundedCorners(radius)))
        Glide.with(view)
            .load(url)
            .apply(options)
            .placeholder(getPlaceholder(context, id))
            .error(getPlaceholder(context, id))
            .into(view)
    }

    /**
     * 加载ImageView scaleType="centerCrop" + 圆角
     */
    fun loaderImgCornersCenterCrop(mContext: Context, url: Any?, view: ImageView, radius: Int, id: Int) {
        val options: RequestOptions = RequestOptions()
            .apply(RequestOptions.bitmapTransform(GlideRoundTransform(mContext, radius)))
        Glide.with(view)
            .load(url)
            .apply(options)
            .placeholder(getPlaceholder(mContext, id))
            .error(getPlaceholder(mContext, id))
            .into(view)
    }

    /**
     * 加载图有-------全局圆角
     */
    fun loaderImgCorners(url: Any, view: ImageView, radius: Int, placeholderId: Int) {
        val options: RequestOptions = RequestOptions()
            .apply(RequestOptions.bitmapTransform(RoundedCorners(radius)))
        Glide.with(view)
            .load(url)
            .apply(options)
            .placeholder(placeholderId)
            .error(placeholderId)
            .into(view)
    }

    /**
     * 加载图有-------顶部圆角
     */
    fun loaderImgCornersTop(url: Any, view: ImageView, radius: Int, context: Context, id: Int) {
        val options: RequestOptions = RequestOptions()
            .apply(RequestOptions.bitmapTransform(GlideTopRoundTransform(view.context, radius)))
        Glide.with(view)
            .load(url)
            .apply(options)
            .placeholder(getPlaceholder(context, id))
            .dontAnimate()
            .error(getPlaceholder(context, id))
            .into(view)
    }

    /**
     * 加载图有-------圆角
     */
    fun loaderImgCornersTop(url: Any, view: ImageView, radius: Int, placeholderId: Int) {
        val options: RequestOptions = RequestOptions()
            .apply(RequestOptions.bitmapTransform(GlideTopRoundTransform(view.context, radius)))
        Glide.with(view)
            .load(url)
            .apply(options)
            .placeholder(placeholderId)
            .dontAnimate()
            .error(placeholderId)
            .into(view)
    }

    fun loaderImgCornersTop(
        url: Any,
        view: ImageView,
        radius: Int,
        width: Int,
        height: Int,
        context: Context,
        id: Int
    ) {
        val options: RequestOptions = RequestOptions()
            .apply(RequestOptions.bitmapTransform(GlideTopRoundTransform(view.context, radius)))
        Glide.with(view)
            .load(url)
            .apply(options)
            .dontAnimate()
            .placeholder(getPlaceholder(context, id))
            .error(getPlaceholder(context, id))
            .override(width, height)
            .into(view)
    }

    fun loadNetCircleCorners(url: String?, img: ImageView, errImgId: Int) {
        Glide.with(img)
            .load(url)
            .error(errImgId)
            .centerCrop()
            .transform(CircleCrop())
            .into(img)
    }

    fun loadNetCircleCornersPlaceholder(url: String?, img: ImageView, placeholderId: Int) {
        Glide.with(img)
            .load(url)
            .placeholder(placeholderId)
            .centerCrop()
            .transform(CircleCrop())
            .into(img)
    }

    /**
     * 加载gif
     */
    fun loadGif(context: Context, url: Int, img: ImageView) {
        Glide.with(context)
            .asGif()
            .load(url)
            .into(img)
    }

    /**
     * 清空内存缓存
     *
     * @param context Context
     */
    fun clearMemory(context: Context) {
        Glide.get(context).clearMemory()
    }


    /**
     * 清空内存缓存
     *
     * @param context Context
     */
    fun clearDiskCache(context: Context) {
        Glide.get(context).clearDiskCache()
    }
}